﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace spaceVibration
{
    class wav
    {
        byte[] Identification = { 0x52, 0x49, 0x46, 0x46 };//资源交换文件标志（RIFF）
        byte[] AllLength = new byte[4];//从下个地址开始到文件尾的总字节数...............
        byte[] WavId = {0x57,0x41,0x56,0x45}; //WAV文件标志（WAVE）
        byte[] FMTId = {0x66,0x6D,0x74,0x20};//波形格式标志（FMT）
        byte[] Filterbyte = {0x10,0x00,0x00,0x00};//过滤字节（一般为00000010H）
        byte[] Pcmformat = {0x01,0x00};//格式种类（值为1时，表示数据为线性PCM编码）
        byte[] ChannelNumber = {0x01,0x00};//通道数，单声道为1，双声音为2
        byte[] Frequency = { 0x80, 0x3e, 0x00, 0x00};//采样频率
        byte[] Speed = {0x40,0x1F,0x00,0x00};//波形数据传输速率（每秒平均字节数）
        byte[] AdjustNumber = {0x02,0x00};//数据的调整数（按字节计算）
        byte[] DigitNumber ={0x10,0x00};//样本数据位数


        byte[] DataID = {0x64,0x61,0x74,0x61};//数据标志符（data）
        byte[] DataLength = new byte[4];//采样数据总数
        byte[] Data;//语音字节流

       

        public FileStream filename;//生产的wav

        //合成声音
        public bool compose(byte[] buffer,int length)
        {
            string filename = "sound_"+ DateTime.Now.ToString("MM_dd_hh_mm")+".wav";

            string path = @"E:/" + filename;
            FileStream fs = new FileStream(path, FileMode.Create);//新建文件


            byte[] AllData = new byte[length+44];
            AllLength = BitConverter.GetBytes(length+36);
            DataLength = BitConverter.GetBytes(length);


            Identification.CopyTo(AllData, 0);//数据融合
            AllLength.CopyTo(AllData, 4);
            WavId.CopyTo(AllData, 8);
            FMTId.CopyTo(AllData, 12);
            Filterbyte.CopyTo(AllData, 16);
            Pcmformat.CopyTo(AllData, 20);
            ChannelNumber.CopyTo(AllData, 22);
            Frequency.CopyTo(AllData, 24);
            Speed.CopyTo(AllData, 28);
            AdjustNumber.CopyTo(AllData, 32);
            DigitNumber.CopyTo(AllData, 34);

            DataID.CopyTo(AllData, 36);
            DataLength.CopyTo(AllData,40);
           
            //buffer.CopyTo(AllData, 44);
            //Array.Copy(AllData, 44, buffer, 0, length);
            Array.Copy(buffer,0,AllData,44,length);
     
            length = length + 44;
            fs.Write(AllData, 0, length);
         
            fs.Flush();
            fs.Close();
       
            return true;
        }
    }
}
